package com.zy.batiya.controller;

import java.io.BufferedReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zy.batiya.service.ApiService;

@Controller
@RequestMapping(value = "/api", produces = "application/json;charset=utf-8")
public class ApiController {
  
  private Logger logger = LoggerFactory.getLogger(ApiController.class);
  
  @Autowired
  private ApiService apiService;
  
  @ResponseBody
  @RequestMapping(value = "/searchInventory")
  public String searchInventory(HttpServletRequest request) {
      JSONObject rs = new JSONObject();
     
      String account = request.getHeader("account");
      String password = request.getHeader("password");
      
      //password check
      if(StringUtils.isNotEmpty(account) && StringUtils.isNotEmpty(password)){
        if(account.equals("tujia2017") && password.equals("9981aee77b63c7dc1456b8c26710e790")){
          
        }else{
          rs.put("result_code", "999");
          rs.put("message", "account or password is wrong");
          logger.info("searchInventory return.." + rs);
          return JSONObject.toJSONString(rs);
        }
      }else{
        rs.put("result_code", "999");
        rs.put("message", "account or password is null");
        logger.info("searchInventory return.." + rs);
        return JSONObject.toJSONString(rs);
      }
      
      try {
          Map<String,String> map = new HashMap<String,String>();
          BufferedReader br = request.getReader();
          String str, wholeStr = "";
          
          while((str = br.readLine()) != null){
            wholeStr += str;
          }
          logger.info("searchInventory.." + wholeStr);
          
          map = JSONObject.parseObject(wholeStr, new TypeReference<Map<String, String>>(){});
       
//          String partner_uid = request.getParameter("partner_uid");
//          String partner_pid = request.getParameter("partner_pid");
//          String checkin = request.getParameter("checkin");
//          String checkout = request.getParameter("checkout");
//          String guest_count = request.getParameter("guest_count");
//          String currency = request.getParameter("currency");
//          String total_price = request.getParameter("total_price");
//          String quantity = request.getParameter("quantity");
//          
//          Map<String, String> paramMap = new HashMap<String,String>();
//          
//          paramMap.put("partner_uid", partner_uid);
//          paramMap.put("partner_pid", partner_pid);
//          paramMap.put("checkin", checkin);
//          paramMap.put("checkout", checkout);
//          paramMap.put("guest_count", guest_count);
//          paramMap.put("currency", currency);
//          paramMap.put("total_price", total_price);
//          paramMap.put("quantity", quantity);
          
          rs = apiService.searchInventory(map);
          
      } catch (Exception e) {
          e.printStackTrace();
      }
      logger.info("searchInventory return.." + rs);
      return JSONObject.toJSONString(rs);
  }
  
  @ResponseBody
  @RequestMapping(value = "/saveOrder")
  public String saveOrder(HttpServletRequest request) {
      JSONObject result = new JSONObject();
      
      String account = request.getHeader("account");
      String password = request.getHeader("password");
      //password check
      if(StringUtils.isNotEmpty(account) && StringUtils.isNotEmpty(password)){
        if(account.equals("tujia2017") && password.equals("9981aee77b63c7dc1456b8c26710e790")){
          
        }else{
          result.put("result_code", "999");
          result.put("message", "account or password is wrong");
          logger.info("saveOrder return.." + result);
          return JSONObject.toJSONString(result);
        }
      }else{
        result.put("result_code", "999");
        result.put("message", "account or password is null");
        logger.info("saveOrder return.." + result);
        return JSONObject.toJSONString(result);
      }
      
      try {
          Map<String,String> map = new HashMap<String,String>();
          BufferedReader br = request.getReader();
          String str, wholeStr = "";
          
          while((str = br.readLine()) != null){
            wholeStr += str;
          }
          logger.info("saveOrder.." + wholeStr);
          
          map = JSONObject.parseObject(wholeStr, new TypeReference<Map<String, String>>(){});      
//          String oid = request.getParameter("oid");
//          String partner_uid = request.getParameter("partner_uid");
//          String partner_pid = request.getParameter("partner_pid");
//          String checkin = request.getParameter("checkin");
//          String checkout = request.getParameter("checkout");
//          String guest_count = request.getParameter("guest_count");
//          String currency = request.getParameter("currency");
//          String total_price = request.getParameter("total_price");
//          String number_of_units = request.getParameter("number_of_units");
//          String link_man = request.getParameter("link_man");
//          String link_phone = request.getParameter("link_phone");
//          String link_email = request.getParameter("link_email");
//          
//          Map<String, String> map = new HashMap<String, String>();
//          map.put("oid", oid);
//          map.put("partner_uid", partner_uid);
//          map.put("partner_pid", partner_pid);
//          map.put("checkin", checkin);
//          map.put("checkout", checkout);
//          map.put("guest_count", guest_count);
//          map.put("currency", currency);
//          map.put("total_price", total_price);
//          map.put("number_of_units", number_of_units);
//          map.put("link_man", link_man);
//          map.put("link_phone", link_phone);
//          map.put("link_email", link_email);
          
          result = apiService.addOrder(map);
          
          logger.info("apiService save order return " + JSONObject.toJSONString(result));
          
          if(result.getString("result_code").equals("0")){
            apiService.updateInventory(map);
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
      logger.info("saveOrder return.." + result);
      return result.toJSONString();
  }
  
  @ResponseBody
  @RequestMapping(value = "/cancelOrder")
  public String cancelOrder(HttpServletRequest request) {
      JSONObject result = new JSONObject();
      
      String account = request.getHeader("account");
      String password = request.getHeader("password");
      //password check
      if(StringUtils.isNotEmpty(account) && StringUtils.isNotEmpty(password)){
        if(account.equals("tujia2017") && password.equals("9981aee77b63c7dc1456b8c26710e790")){
          
        }else{
          result.put("result_code", "999");
          result.put("message", "account or password is wrong");
          logger.info("cancelOrder return.." + result);
          return JSONObject.toJSONString(result);
        }
      }else{
        result.put("result_code", "999");
        result.put("message", "account or password is null");
        logger.info("queryOrder return.." + result);
        return JSONObject.toJSONString(result);
      }
      
      try {
          Map<String,String> map = new HashMap<String,String>();
          BufferedReader br = request.getReader();
          String str, wholeStr = "";
          
          while((str = br.readLine()) != null){
            wholeStr += str;
          }
          logger.info("cancelOrder .." + wholeStr);
          
          map = JSONObject.parseObject(wholeStr, new TypeReference<Map<String, String>>(){}); 
        
//          String partner_oid = request.getParameter("partner_oid");
//          
//          Map<String, String> map = new HashMap<String, String>();
//          map.put("oid", partner_oid);
          String partner_oid = map.get("partner_oid");
          result = apiService.cancelOrder(map);
          
          if(result.getString("result_code").equals("0")){
            apiService.updateInventoryItemByCancel(partner_oid);
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
      logger.info("cancelOrder return.." + result);
      return JSONObject.toJSONString(result);
  }
  
  @ResponseBody
  @RequestMapping(value = "/queryOrder")
  public String queryOrder(HttpServletRequest request) {
      JSONObject rs = new JSONObject();
      
      String account = request.getHeader("account");
      String password = request.getHeader("password");
      //password check
      if(StringUtils.isNotEmpty(account) && StringUtils.isNotEmpty(password)){
        if(account.equals("tujia2017") && password.equals("9981aee77b63c7dc1456b8c26710e790")){
          
        }else{
          rs.put("result_code", "999");
          rs.put("message", "account or password is wrong");
          logger.info("queryOrder return.." + rs);
          return JSONObject.toJSONString(rs);
        }
      }else{
        rs.put("result_code", "999");
        rs.put("message", "account or password is null");
        logger.info("queryOrder return.." + rs);
        return JSONObject.toJSONString(rs);
      }
      
      try {
//          String partner_oid = request.getParameter("partner_oid");
//          Map<String, String> paramMap = new HashMap<String, String>();
//          paramMap.put("oid", partner_oid);
          
          Map<String,String> map = new HashMap<String,String>();
          BufferedReader br = request.getReader();
          String str, wholeStr = "";
          
          while((str = br.readLine()) != null){
            wholeStr += str;
          }
          logger.info("queryOrder .." + wholeStr);
          
          map = JSONObject.parseObject(wholeStr, new TypeReference<Map<String, String>>(){}); 
          
          rs = apiService.searchOrder(map);
      } catch (Exception e) {
          e.printStackTrace();
      }
      logger.info("queryOrder return.." + rs.toJSONString());
      return rs.toJSONString();
  }
  
  @ResponseBody
  @RequestMapping(value = "/getical")
  public String getIcal(HttpServletRequest request) {
//      String rs = "BEGIN:VCALENDAR" + "\n" +
//            "VERSION:2.0" + "\n" +
//              "PRODID:-//Booking.com, b.v.//NONSGML v1.0//EN" + "\n" +
//                "CALSCALE:GREGORIAN" + "\n" +
//                  "METHOD:PUBLISH" + "\n" +
//                    "BEGIN:VEVENT" + "\n" +
//                      "DTSTART;VALUE=DATE:20180401" + "\n" +
//                      "DTEND;VALUE=DATE:20180410" + "\n" +
//                      "UID:778e7f240dbcb73be5c43ea2f88a8466@booking.com" + "\n" +
//                      "SUMMARY: BOOKED" + "\n" +
//                      "END:VEVENT" + "\n" +
//                      "END:VCALENDAR";
//      return rs;
    return "";
  }
}
